<?php

namespace mpend\modules\member\controllers;

use Yii;
use mpend\components\Controller;
use mpend\modules\member\models\MemberDataSearch;
use mpend\modules\member\models\MemberApplyForm;
use yii\helpers\Inflector;

/**
 * 会员类型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ApplyController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'index' => ['GET'],
            '*' => ['POST'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actionIndex() {
        return MemberDataSearch::findByUserId($this->uid);
    }

    /**
     * @inheritdoc
     */
    public function actionCreate() {
        $model = new MemberApplyForm();
        $params = $this->formatPost(Yii::$app->request->post());
        if (!$model->load($params, '')) {
            parent::warning('数据未加载成功');
        }
        $model->user_id = $this->uid;
        return $model->save();
    }

    protected function formatPost($post) {
        $params = [];
        foreach ($post as $key => $vls) {
            $name = Inflector::camel2id($key, '_');
            $params[$name] = $vls;
        }
        return $params;
    }

}
